using GriffinNav.GPS;
using GriffinNav.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GriffinNav.UI
{
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();
            Text = Global.Name;

            try
            {
                // check for available COM ports
                if (Global.SerialPorts != null)
                {
                    // add COM ports to drop down
                    foreach (string port in Global.SerialPorts)
                    {
                        cboPort.Items.Add(port);

                        if (port == AppSettings.GpsPort)
                        {
                            // display current port setting
                            cboPort.SelectedItem = port;
                        }
                    }
                }

                if (AppSettings.GpsReadTimeout > 0 && AppSettings.GpsReadTimeout <= 60)
                {
                    numGpsTimeout.Value = AppSettings.GpsReadTimeout;
                }

                if (AppSettings.GpsBaudRate > 0)
                {
                    // display current baud rate setting
                    cboBaudRate.SelectedItem = AppSettings.GpsBaudRate.ToString();
                }

                if (AppSettings.UnitSystem == UnitSystem.English)
                {
                    cboUnit.SelectedItem = "English";
                }
                else if (AppSettings.UnitSystem == UnitSystem.Metric)
                {
                    cboUnit.SelectedItem = "Metric";
                }
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        public void ShowDialog(OptionsTab tab)
        {
            // select tab
            tabOptions.SelectedIndex = (int)tab;

            base.ShowDialog();
        }

        private void cboPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // check if selection is different from current setting
                if (AppSettings.GpsPort != cboPort.SelectedItem.ToString())
                {
                    if (Global.Gps != null)
                    {
                        // stop the GPS if running
                        Global.Gps.StopGps();
                    }

                    // change the COM port setting
                    AppSettings.GpsPort = cboPort.SelectedItem.ToString();
                }
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        private void cboBaudRate_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int baudRate = int.Parse(cboBaudRate.SelectedItem.ToString());

                // check if the selection is different from the current setting
                if (AppSettings.GpsBaudRate != baudRate)
                {
                    if (Global.Gps != null)
                    {
                        // stop the GPS if running
                        Global.Gps.StopGps();
                    }

                    // change the baud rate setting
                    AppSettings.GpsBaudRate = baudRate;
                }
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        private void cboUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                // change unit system setting
                if (cboUnit.SelectedItem.ToString().ToLower().Equals("metric"))
                {
                    AppSettings.UnitSystem = GriffinNav.GPS.UnitSystem.Metric;
                }
                else if (cboUnit.SelectedItem.ToString().ToLower().Equals("english"))
                {
                    AppSettings.UnitSystem = GriffinNav.GPS.UnitSystem.English;
                }
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }

        private void numGpsTimeout_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                int timeout = (int)numGpsTimeout.Value;

                // check if the selection is different from the current setting
                if (AppSettings.GpsReadTimeout != timeout)
                {
                    if (Global.Gps != null)
                    {
                        // stop the GPS if running
                        Global.Gps.StopGps();
                    }

                    // change the baud rate setting
                    AppSettings.GpsReadTimeout = timeout;
                }
            }
            catch (Exception ex)
            {
                Global.ShowError("Error", ex.Message);
            }
        }
    }
}